﻿/**
 * columnsChart
 * Charts Collection
 *
 * @author		Duarte Peixinho, mail: duarte.peixinho@gmail.com
 * @version		1.0
 */

package com.duartepeixinho.charts.chartsType
{
	import com.duartepeixinho.charts.grid.ChartGrid
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import com.duartepeixinho.ptween.Ptween;
	
	public class ColumnsChart extends ChartGrid
	{
		//const		
		private const COLUMNS_DEFAULT_WIDTH:uint = 3;
		//vars
		private var _values:Array;
		private var _animation:Boolean;
		
		/**
		 * Creates a Column Chart.
		 * 
		 * @param		values				Array		Schema of the array: [ x:Number, y:Number, columnWidth:uint, color:uint , label:String, label2:String ]
		 * @param		animation			Boolean		Use animation on the chart (Ptween)
		 * @param		coordEnd			Point		Point of the width and heigth if the axis
		 * @param		CountInit			Point		Point that determines the starting number of each axis
 		 * @param		CountEnd			Point		How many numbers in each axis
		 * @param		xCount				Number		Increase Number of the XX axis
		 * @param		yCount				Number		Increase Number of the YY axis
		 * @param		showXMarks			Boolean		Show Labels of the XX axis
		 * @param		showYMarks			Boolean		Show Labels of the YY axis
		 * @param		xLabel				String		String to appear as the label of the XX axis
		 * @param		yLabel				String		String to appear as the label of the YY axis
		 * @param		grid				Boolean		Show the background grid or not
		 * @param		showZero			Boolean		Show the 0 on the corner or not
		 * @param		title				String		Title of the chart
		 */
		
		public function ColumnsChart(values:Array, animation:Boolean, coordEnd:Point, CountInit:Point, CountEnd:Point, xCount:Number, yCount:Number, showXMarks:Boolean, showYMarks:Boolean, xLabel:String = null, yLabel:String = null, grid:Boolean = true, showZero:Boolean = false, title:String=null )
		{
			//instantiates the parent constructor
			super(coordEnd, CountInit, CountEnd, xCount, yCount, showXMarks, showYMarks, xLabel, yLabel, grid, showZero, title);
			_values = values;
			_animation = animation;
			
			//draw the columns
			buildColumns();
		}		
		
		/*
		 * Draw the columns
		 */ 
		private function buildColumns():void {
			
			//sets the var color
			var pointColor:uint;
			
			//getting colors automatically
			var colorsPallette:Number = 10777215;
			//convert dec to hex: (16777215).toString(16);
			
			
			for (var i:int = 0; i < _values.length; i++) {
				if (_values[i][3].length == 0) {
					//sets the color
					_values[i][3] = "0x" + (colorsPallette / _values.length * (i+1)).toString(16);
				}
			}
			
			//loop to draw the columns
			for (i = 0; i < _values.length; i++) {
				//gets the previous set color
				pointColor = _values[i][3];				
				var shape:Shape = new Shape();
				var container:Sprite = new Sprite();
				shape.graphics.beginFill(pointColor);
				
				var pointLocation:Point = convertCoordChart(new Point(_values[i][0], 0));
				var widthHeightConvert:Point;
				
				//setting width and height
				if (_values[i][2]) {			
					widthHeightConvert = convertCoordChart(new Point(_values[i][0] + _values[i][2], _values[i][1]));
				}
				else {
					widthHeightConvert = convertCoordChart(new Point(_values[i][0] + COLUMNS_DEFAULT_WIDTH, _values[i][1]));
				}
								
				//creating shape
				shape.graphics.drawRect(0, 0, widthHeightConvert.x - pointLocation.x, widthHeightConvert.y - pointLocation.y);
				shape.graphics.endFill();
				
				//set container coordinates
				container.x = pointLocation.x; 
				container.y = pointLocation.y;
				container.addChild(shape);
				addChild(container);
				
				//setting the point label if exists in the array
				if (_values[i][4]) {
					//sets the label
					var label:TextField = new TextField();
					label.text = _values[i][4];
					label.setTextFormat(new TextFormat(null, 9, pointColor));
					label.height = label.textHeight + 5;
					label.y = pointLocation.y;
					label.x = pointLocation.x + (widthHeightConvert.x - pointLocation.x) / 2 - label.textWidth / 2;					
					addChild(label);
					if (_animation) {
						Ptween.addTween(label, { y: widthHeightConvert.y -20, time:3, transition:"easeOutElastic" } );
					}
				}
				
				//setting the label of the column
				if (_values[i][5]) {
				//sets the label
					var label2:TextField = new TextField();
					label2.text = _values[i][5];
					label2.setTextFormat(new TextFormat(null, 9, pointColor));
					label2.height = label2.textHeight + 5;
					var pointLocation2:Point = convertCoordChart(new Point(0, 0));
					label2.y = pointLocation2.y+2;
					label2.x = pointLocation.x;					
					addChild(label2);
				}
				//creating animation if setted
				if (_animation) {
					container.scaleY = 0;
					//animation using Tweener
					Ptween.addTween(container, { scaleX:1, scaleY:1, time:3, transition:"easeOutElastic" } );
				}
			}
		}
	}
	
}